//
//  CommentAlertView.m
//  xpkc
//
//  Created by 胡志飞 on 15/11/5.
//  Copyright © 2015年 TopSage. All rights reserved.
//

#import "CommentAlertView.h"
#import "UserMessage.h"
@interface CommentAlertView ()<UITextViewDelegate>



@end


@implementation CommentAlertView
{
    UITextView *textView;
    UIView *backgroudView;
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 8;
        self.userInteractionEnabled=YES;
        backgroudView.userInteractionEnabled=YES;
        [self p_setUp];
    }
    return self;

}
/**
 *  布局子视图
 */
- (void)p_setUp {
    
    
    
    CGFloat bottomInset = 8;// 底部间距
    CGFloat leftInset = 10; // button左边间距
    CGFloat midInset = 20;  // 两个button之间距离
    CGFloat textViewInset = 5; // button与textView间距
    CGFloat buttonW = (self.frame.size.width-leftInset*2-midInset)*0.5;
    CGFloat buttonH = buttonW/3;
    
    // 提交按钮
    UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    commitBtn.frame = CGRectMake(leftInset,self.frame.size.height-buttonH-bottomInset, buttonW, buttonH);
    commitBtn.backgroundColor = [UIColor colorWithRed:251/255.0 green:134/255.0 blue:42/255.0 alpha:1.0];
    [commitBtn setTitle:@"提交" forState:UIControlStateNormal];
    [commitBtn addTarget:self action:@selector(commitClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:commitBtn];
    
    //取消按钮
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(leftInset+buttonW+midInset,CGRectGetMinY(commitBtn.frame),buttonW, buttonH);
    cancelBtn.backgroundColor = [UIColor colorWithRed:242/255.0 green:243/255.0 blue:244/255.0 alpha:1.0];
    [cancelBtn setTitle:@"取消"forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelInput) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];
    
    //输入框
    textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-bottomInset-buttonH-textViewInset)];
    textView.text = @"请给出您对本课程的评价";
    textView.layer.masksToBounds = YES;
    textView.layer.cornerRadius = 8;
    textView.font = [UIFont systemFontOfSize:20];
    textView.textColor = [UIColor colorWithRed:194/255.0 green:195/255.0 blue:196/255.0 alpha:1.0];
    textView.delegate = self;
    
    [self addSubview:textView];


}

-(void)commitClick
{
    [textView resignFirstResponder];
    
    NSString *module = @"openCourse";
    NSString *interface = @"opencoursecomment";
    
    NSString *timeString = [GetTime getTime];
    NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
    NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
    
    NSDictionary *parameter = @{@"sid":self.videoId,@"mid":[UserMessage sharedUserMessage].userMid,@"content":textView.text,@"pid":@0,@"star":self.star};
//    NSLog(@"%@",parameter);
    //POST请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        if ([[dic objectForKey:@"succ"] isEqual:@1]) {
             ShowAlertMsg(@"评价成功，请等待通过审核");
              [self removeFromSuperview];
        }else{
            ShowAlertMsg(@"评价失败");
        }
       
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        ShowAlertMsg(@"评价失败");
    }];

    
    
    
    
    
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [textView resignFirstResponder];

}
/**
 * 弹出视图
 */
- (void)show {

    //背景幕布
    backgroudView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    backgroudView.backgroundColor = [UIColor blackColor];
    backgroudView.alpha = 0.5;
//    [[UIApplication sharedApplication].keyWindow addSubview:backgroudView];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    }


-(BOOL)textViewShouldEndEditing:(UITextView *)textview
{
    [textView resignFirstResponder];
    return YES;

}

/**
 *  移除视图
 */
- (void)cancelInput {
    [textView resignFirstResponder];
    [self removeFromSuperview];
//    [backgroudView removeFromSuperview];
 

}

/**
 *  textView代理方法，将要开始输入时调用，用来清除占位字符
 */

- (BOOL)textViewShouldBeginEditing:(UITextView *)textview {
    
    textview.text = @" ";
    return YES;
}

@end
